package cn.gjxblog.service.impl;

import cn.gjxblog.common.model.PageCondition;
import cn.gjxblog.dao.AgencyMapper;
import cn.gjxblog.dao.UserMapper;
import cn.gjxblog.model.entity.Agency;
import cn.gjxblog.model.entity.AgencyExample;
import cn.gjxblog.model.entity.User;

import cn.gjxblog.model.entity.UserExample;
import cn.gjxblog.service.AgencyService;
import cn.gjxblog.service.UserService;
import cn.gjxblog.common.utils.UUIDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2018/12/18 11:31
 */
@Service
public class AgencyServiceImpl implements AgencyService {

    @Resource
    private AgencyMapper agencyMapper;

    @Resource
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @Override
    public void add(Agency agency) {
        agency.setId(UUIDUtils.get32LUUid());
        agencyMapper.insertSelective(agency);
    }

    @Override
    public List<Agency> getAllAgency() {
        List<Agency> agencies = agencyMapper.selectByExample(new AgencyExample());
        return agencies;
    }

    @Override
    public User getAgentDetail(String id) {
        User user = userService.getUserById(id);

        Agency agency = agencyMapper.selectByPrimaryKey(user.getAgencyId());
        if(agency !=null){
            user.setAgencyName(agency.getName());
        }
        return user;
    }



    @Override
    public Agency getAgencyDetail(String id) {
        Agency agency = agencyMapper.selectByPrimaryKey(id);
        return agency;
    }

    @Override
    public PageInfo<User> getAgentsByCondition(PageCondition pageCondition) {
        PageHelper.startPage(pageCondition.getPageNum(),pageCondition.getPageSize());
        UserExample userExample = new UserExample();
        userExample.createCriteria().andTypeEqualTo(2);
        List<User> users = this.userMapper.selectByExample(userExample);
        users.stream().forEach(user->{
            Agency agency = agencyMapper.selectByPrimaryKey(user.getAgencyId());
            if(agency !=null){
                user.setAgencyName(agency.getName());
            }
        });
        PageInfo<User> userPageInfo = new PageInfo<>(users);

        return userPageInfo;
    }
}
